﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio.Security
{
    /// <summary>
    /// Base64 加密解密算法
    /// </summary>
    [ComVisible(true)]
    [ComSourceInterfaces(typeof(ISecurity))]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class SecurityBase64 : ISecurity
    {
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="argEncryptString">待加密的密文</param>
        /// <returns>加密字符串</returns>
        public string Encrypt(string argEncryptString)
        {
            try
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(argEncryptString);
                return Convert.ToBase64String(inputBytes);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="argDecryptString">待解密的密文</param>
        /// <returns>解密字符串</returns>
        public string Decrypt(string argDecryptString)
        {
            try
            {
                byte[] inputBytes = Convert.FromBase64String(argDecryptString);
                return Encoding.UTF8.GetString(inputBytes);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}
